<?php
namespace jy_weishop\page;
defined('BY_JYA') or exit('error');
/*
	页面返回
*/
class index extends \plugin{
	public function getmain($return=array()){
		global $_RQ;
		$return['show_setmember']=cfc('core')->check_setmember(1);
		if($_RQ['fromuser']){
			$this->model('fenxiao')->up_fenfee($return['shopset']);
		}
		//更新客户归属
		if($_RQ['issale']){
			$id=intval($_RQ['id']);
			$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
			model('jy_weishop/store')->up_memberid($id,'',$_RQ['workerid']);
		}
		unset($_SESSION['ocreateurl']);
		$return['list']=array();
		$return['indexpai']=array();	
		$setting = $return['setting'];
		$return['welcomehdps']=$setting['welcomehdps'];
		if(count($setting['welcomehdps'])>0 && $setting['iswelcome']==1){
			$return['iswelcome']=$this->session('iswelcome')?0:1;
			if($return['iswelcome']){
				$this->session('iswelcome',1);
			}
		}
		if(!$setting['issearch']){
			$return['indexpai'][]=array('name'=>'search');
		}
		if(!$setting['ishdps']){
			$return['indexpai'][]=array('name'=>'hdps');
		}
		$return['indexpai'][]=array('name'=>'notice');
		if(!$setting['isswiper']){
			$return['indexpai'][]=array('name'=>'swiper');
		}
		if(!$setting['isbankuai1']){
			$return['indexpai'][]=array('name'=>'bankuai1');
		}
		if(!$setting['isgroups']){
			$return['indexpai'][]=array('name'=>'groups');
		}
		if(!$setting['iscube']){
			$return['indexpai'][]=array('name'=>'cube');
		}
		if(!$setting['isshare']){
			$return['indexpai'][]=array('name'=>'share');
		}
		if(!$setting['no_ordermodal']){
			$return['indexpai'][]=array('name'=>'ordermodal');
		}
		if(!$setting['iscoupon']){
			$return['indexpai'][]=array('name'=>'coupon');
		}
		$return['indexpai'][]=array('name'=>'goods');
		$groupspram=array('uniacid'=>$_SESSION['uniacid'],'isrecommand'=>'1','isshow'=>'1','mid'=>0,'parentid'=>0);
		$groupspram['onepluginname']=$_RQ['pluginname']?:'';
		$p_group=pdo_getall('jy_weishop_goods_group',$groupspram,'','','paixu desc');
		//子分类
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
		$params['saletype likes']=array('1');
		$pindex = max(1, intval($_RQ['page']));
		$psize = 10;
		$total='';
		$fields=array('id','title','image','price','total','active_plugin','active_id');
		if($p_group){
			$i=0;
			$style=array('style-brown','style-red','style-pink','style-orange','style-green','style-blue','style-purple','style-brown');
			foreach($p_group as $k=>&$item){
				$item['c_group']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','parentid'=>$item['id']),array(),'','paixu desc');
				if($_SESSION['os']=='windows'){
					$item['goodslist']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('1'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
					$item['goodsnew']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('2'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
					$item['style']=$style[$k];
				}
				$i=$i+1;
				$item['i']=$i;
				if($item['c_group']){
					foreach($item['c_group'] as &$row){
						$params['ccate']=$row['id'];
						if($_SESSION['os']=='windows'){
							$row['goods']=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc');
						}
					}
				}
			}
		}
		//公告列表
		$notice_list = pdo_getslice('jy_weishop_notice',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,20),$total,array('id','title','createtime'),'','id desc');
		if($notice_list){
			foreach($notice_list as &$notice){
				if($_SESSION['os']=='windows'){
					$notice['createtime']=date('m-d',$notice['createtime']);
				}else{
					$notice['createtime']=date('Y-m-d h:i',$notice['createtime']);
				}
			}
		}
		$pindex = max(1, intval($post['page']));
		$psize = 10;
		$total='';
		//获取用户信息
		$return['extradata']=array();
		$return['user']=model('core/member')->basic($_SESSION['uid']);
		$return['login']=$_SESSION['uid']?1:2;
		$return['notice_list']=$notice_list;
		$return['p_group']=$p_group;
		$return['params']=array('pid'=>$_RQ['pid']?:0,'order'=>$_RQ['order']?:'isrecommand');
		$return['feemoney']=$this->plugin_setting('feemoney');
		$return['pcd']=$this->session('pcd')?:array('city'=>'');
		$jy_merch = $this->plugin_exists('jy_merch');
		if($jy_merch){
			$return['jy_merch']=true;
		}
		$sale_luck=plugin('jy_weishop')->plugin_setting('sale_luck');
		if($sale_luck['isopen']==1){
				if(isset($_RQ['luckkey'])){
					$return['sale_luck']=model('jy_weishop/activity/sales')->jieluck($_RQ['luckkey']);
				}else{
					$return['sale_luck']=model('jy_weishop/activity/sales')->getluck();
				}
		}
		if(!$return['sale_luck']){
			$return['sale_tan']=model('jy_weishop/activity/sales')->gettan(0);
			if(!$return['sale_tan']) {
				$isshow_sale_offer = $this->session('isshow_sale_offer');
				if (!$isshow_sale_offer) {
					$return['sale_offer'] = model('jy_weishop/activity/sales')->getoffer();
				}
			}
			if(!$return['sale_offer']){
					$isshow_sale_new=$this->session('isshow_sale_new');
					if(!$isshow_sale_new){
						$return['sale_new']=model('jy_weishop/activity/sales')->getnew();
					}
					if(!$return['sale_new']){
						$return['orderlist']=pdo_getslice('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),array($pindex,$psize),$total,'','','createtime desc');
						foreach($return['orderlist'] as &$ov){
							$ordergoods=pdo_get('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
							$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$ordergoods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
							$ov['goods']=array_merge($ordergoods,$goodsinfo);
							$ov['createtime']=date('Y/m/d H:i:s',$ov['createtime']);
							$ov['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),'total');
						}
						$return['allprice']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),'price');
					}else{
						$this->session('isshow_sale_new',1);
					}
			}else{
						$this->session('isshow_sale_offer',1);
			}
		}
		$return['coupon_wait']=model('jy_weishop/task')->pop_coupon_wait();
		$return['coupon_wait']=$return['coupon_wait']?$return['coupon_wait'][0]:false;
		if(!$return['coupon_wait']){
			$return['coupon_hd']=model('jy_weishop/task')->pop_coupon_hd();
			if(!$return['coupon_hd']){
				$return['game_wait']=model('jy_weishop/task')->pop_game();
			}
		}
		if($return['sale_luck']){
			$sharelink='jy_weishop/index//fromuser='.$_SESSION['uid'].'&luckkey='.$return['sale_luck']['luckkey'];
			if($sale_luck['share_title']){
				$sale_luck['share_title']=str_replace('[lucktitle]',$return['sale_luck']['title'],$sale_luck['share_title']);
			}
			if($sale_luck['share_desc']){
				$sale_luck['share_desc']=str_replace('[lucktitle]',$return['sale_luck']['title'],$sale_luck['share_desc']);
			}
			$return['sharedata']=array(
				'title'=>$sale_luck['share_title']?$sale_luck['share_title']:'我今日运势《'.$return['sale_luck']['title'].'》，快来借势吧,好运势享大折扣！',
				'desc'=>$sale_luck['share_desc']?$sale_luck['share_desc']:'拥有好运势，不仅仅拥有好心情，更能享受好的购物折扣哦！',
				'link'=>custom_url($sharelink,true),
				'imgUrl'=>$sale_luck['share_image']?tomedia($sale_luck['share_image']):tomedia('../addons/jy_weishop/resource/images/sales/luck_share_image.jpg')
			);
		}else{
			$sharelink='jy_weishop/index//fromuser='.$_SESSION['uid'];
			$return['sharedata']=array(
				'title'=>$setting['share_title']?$setting['share_title']:'首页-在线商城',
				'desc'=>$setting['share_desc']?$setting['share_desc']:'在线商城，一个属于自己的个人商城！',
				'link'=>custom_url($sharelink,true),
				'imgUrl'=>tomedia($setting['share_image'])
			);
		}
		$isfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');	
		if(!$isfollow && !$return['shopset']['noshowqrcode'] && $_SESSION['uid']){
			$return['showwechatcode']=1;
		}
		$return['member']=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>0),array('mid','level'));
		$return['nodata_pic']='../core/resource/images/index_nodata.jpg';
		if(intval($setting['index_tpl']) && $_SESSION['os'] != 'windows'){
			$return['htmls']=$this->template_url('index_a'.$setting['index_tpl']);
		}else{
			$return['setting']['index_tpl']=3;
		}
		return $return;
	}
}